package pcjavanet.mobilesell.web.action;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import pcjavanet.mobilesell.model.QryCondition;
import pcjavanet.mobilesell.model.QryTerm;

public class RequestUtil {
 
	public static String [] getWordByBigC(String fieldName){
		int b1 = 0;
		int b2 = 0;
		int b3 = 0;
		String [] strs = new String[4];
		for (int j = 0; j < fieldName.length(); j++) {
			boolean add = false ; 
			char c = fieldName.charAt(j);
			// A =65 Z=90 a=97 z=122
			if (c >= 65 && c <= 90) {
				if (b1 == 0) {
					b1 = j;
					add = true ; 
				}
				if ( add== false &&  b2 == 0) {
					b2 = j;
					add = true ; 
				}
				if (add == false && b3 == 0) {
					b3 = j;
					add = true ; 
				}
				 
			}	
		}
 
		strs[0] = fieldName.substring(0,b1);
		strs[1] = fieldName.substring(b1,b2);
		strs[2] = fieldName.substring(b2,b3);
		strs[3] = fieldName.substring(b3,fieldName.length());
		
		return strs;
	}
	
	public static void main(String[] args){
		System.out.println(  Arrays.asList( getWordByBigC( "qryEndDateCreateDate") )) ;
	}
	
	// qryBeginDateCreateDate  qry  Begin  Date   createDate
	// qryEndDateCreateDate    qry    End    Date   createDate
	// qryBeginIntAge          qry  Begin  Int    age
	// qryBeginStringName	   qry  Begin  String name
	public static  QryCondition getQryCondition( HttpServletRequest request ){
		QryCondition qry = new QryCondition();
 
		qry.setQryLikeEnable( request.getParameter( QryCondition.LIKE_ENABLE ));
		if ( request != null ){
			Enumeration<String> nameEnum = request.getParameterNames();
			while ( nameEnum.hasMoreElements() ){
				String name =(String) nameEnum.nextElement() ; 
				if ( name.indexOf("qry") != -1  &&  (!name.equalsIgnoreCase(QryCondition.LIKE_ENABLE) )) {
					String value = "";
					try {
						value = new String( request.getParameter(name).getBytes("ISO8859-1"),"UTF-8");
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
					String []  qNames= getWordByBigC(name);
					 String qName = qNames [ 3 ] ;
					 String qType = qNames [ 2 ] ;
					 String qPosition = qNames [ 1 ] ;
					 QryTerm term =  qry.getQryMap().get( qName ) ; 
					 if ( term == null ){
						 term = new QryTerm();
						 term.setName(qName);
						 term.setType(qType);
					 }
					 term.setValue(value, qPosition);
					 qry.getQryMap().put(qName, term);
				}
			}
		}
		return qry ;
	}
}
